package server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import controller.IMenuController;
import controller.IRemoteController;
import controller.RemoteController;
import entity.IData;

public class Listener implements Runnable, IListener {

	private int port;
	private IData data;
	private IMenuController mc;

	public Listener(int port, IData data, IMenuController mc){
		this.port = port;
		this.data = data;
		this.mc = mc;
	}

	@Override
	public void run() {
		Socket clientSocket = null;
		try ( ServerSocket serverSocket = new ServerSocket(port); )
		{
			while (true){
				clientSocket = serverSocket.accept();
				IRemoteController rc = new RemoteController(data, clientSocket,mc); 
				Thread remoteThread = new Thread((Runnable) rc);
				remoteThread.start();
				}
		} catch (IOException e1) {
			System.out.println("ServerSocket fejlede. Genstart programmet og prøv igen.");
			e1.printStackTrace();
			System.exit(1);
		} 
	}
}

